#include "Table.h"
#include "Engine.h"
#include "StageManager.h"

const char Table::className[] = "Table";

#define method(class, name) {#name, &class::name}

Lunar<Table>::RegType Table::methods[] = {
	method(Table, load),
};

Table::Table() {
}

Table::Table(lua_State* L) {
}

Table::~Table() {
}

void Table::init() {
}

int Table::load(lua_State* L) {
	// Read table position
	float x,y,z;
	std::string modelFile = "../data/mesh/";

	int nbArgs = lua_gettop(L);
	if (nbArgs == 4 && lua_isnumber(L,1) && lua_isnumber(L,2) && lua_isnumber(L,3) && lua_isstring(L,4)) {
		x = lua_tonumber(L,1);
		y = lua_tonumber(L,2);
		z = lua_tonumber(L,3);
		modelFile += lua_tostring(L,4);
	}
	else {
		std::cerr << "Table loading : wrong parameters" << std::endl;

		return 0;
	}

	// Load flowerpot model
	osg::PositionAttitudeTransform *transform = new osg::PositionAttitudeTransform;
	osg::Node* table = osgDB::readNodeFile(modelFile);
	transform->addChild(table);
	transform->setPosition(osg::Vec3(x, y, z));
	//transform->setAttitude(osg::Quat(osg::PI_2, osg::Vec3d(1.0, 0.0, 0.0)));

	StageManager::getInstance().getRootNode()->addChild(transform);

	return 0;
}


